#include <iostream>
using namespace std;

#include <pthread.h>
#include <unistd.h>

void *thread_func(void *arg)
{
    int cnt = *(int*)arg;
    while(cnt--)
    {
        cout<<"hello world"<<endl;
        sleep(1);
    }
    //线程退出
    pthread_exit(NULL);
}
int main()
{
    //初始化线程状态
    pthread_attr_t threadAttr;
    pthread_attr_init(&threadAttr);


    //设置线程的分离状态
    int ret = pthread_attr_setdetachstate(&threadAttr,PTHREAD_CREATE_DETACHED);
    if(ret != 0)
    {
        perror("setdetachstate error:");
        _exit(-1);
    }

    pthread_t threadId;
    ret = pthread_create(&threadId,&threadAttr,thread_func,NULL);
    //释放线程属性
    pthread_attr_destroy(&threadAttr);
}